package org.ky.controller;

import org.ky.entity.SmbmsUser;
import org.ky.service.SmbmsUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.FileDescriptor;
import java.util.List;

@Controller
@RequestMapping("/user")
public class LoginController {

    //将业务层注入
    @Resource
    private SmbmsUserService smbmsUserService;

    //登录的接口
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session) {
        System.out.println(username + " " + password);
        boolean login = smbmsUserService.login(username, password);
        System.out.println("---------2------------");
        if (login) {
            //成功之后跳转首页

            //当前这个用户的用户名知不知道？ session 一次会话
            //可以将uname存储起来.
            session.setAttribute("name", username);
            System.out.println("------3-----------");
            return "welcome";
        } else {
            //失败继续登录
            System.out.println("--------4---------");
            return "login";
        }
    }

    //查询
    @RequestMapping("/showList")
    public String showList(Model model){
        System.out.println("------2.1-----------");
        List<SmbmsUser> list = smbmsUserService.showList();

        for(SmbmsUser smbmsUser:list){
            System.out.println(smbmsUser.getRoles().toString());
        }

        if(list.size()>0){
            //将查询到的数据list放入model
            model.addAttribute("list", list);
            System.out.println("------2.2-----------");
            return "userList";
        }else{
            System.out.println("------2.3-----------");
            return "error";
        }
    }



}